/**
 * @desc
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/12/13
 **/

let inquirer = require('inquirer');

let questions = [
  {
    type: 'list',
    name: 'selection',
    message: '请选择环境------------>',
    choices: [
      { name: 'test' },
      { name: 'dev' },
      { name: 'gray' },
      { name: 'prod' },
    ],
  },
];

export function getEnv() {
  let envCode = null;
  process.argv.forEach(function(val, index, array) {
    if (val.startsWith('-e')) {
      envCode = val.replace('-e=', '');
      return false;
    }
  });

  if (envCode) {
    return new Promise(resolve => {
      console.log('用户选择环境编码envCode:', envCode);
      resolve(envCode);
    });
  } else {
    return inquirer
      .prompt(questions)
      .then(answers => {
        let envCode = answers.selection;
        console.log('用户选择环境编码envCode:', envCode);
        return { envCode };
      })
      .catch(err => console.log(err));
  }
}

export function getPlatform() {
  let questions = [
    {
      type: 'list',
      name: 'selection',
      message: '请选择平台------------>',
      choices: [{ name: 'weixin' }, { name: 'pc' }],
    },
  ];

  return inquirer
    .prompt(questions)
    .then(answers => {
      let platform = answers.selection;
      console.log('用户选择平台编码:', platform);
      return { platform };
    })
    .catch(err => console.log(err));
}
